<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/6/006
 * Time: 15:15
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\OrderBuy as OrderBuyModel;
use app\api\model\OrderBuyAfterSale as OrderBuyAfterSaleModel;
use app\api\service\OrderBuyAfterSale as OrderBuyAfterSaleService;
use app\api\validate\ExpressValidate;
use app\api\validate\IDMustBePostiveInt;
use app\api\validate\OrderAftermarketValidate;
use app\api\validate\OrderValidate;
use app\lib\exception\OrderException;
use app\lib\exception\SuccessMessage;

class OrderBuy extends BaseController {

    /**
     * 获取用户商城订单列表
     * @param int $status
     * @param int $comment_status
     * @return \think\Paginator
     * @throws OrderException
     */
    public function getMemberOrderBuyList($status = 0, $comment_status = 0) {
        // 可选参数，如果传了就要验证
        if($status > 0) (new OrderValidate)->goCheck('status');
        if($comment_status > 0) (new OrderValidate)->goCheck('comment_status');

        $order = OrderBuyModel::getMemberOrderBuyList($status, $comment_status);
        $order = $order->toArray();
        return $order;
    }

    /**
     * 确认商城订单数据
     * @param $id
     * @param int $attr_group_id
     * @param int $num
     * @return array
     * @throws OrderException
     */
    public function bookingQuantityBody($id, $attr_group_id, $num = 1) {
        (new IDMustBePostiveInt())->goCheck();
        (new OrderValidate())->goCheck('attr_group_id');
        if ($num > 1) (new OrderValidate())->goCheck('num');

        $order = OrderBuyModel::bookingQuantityBody($id, $attr_group_id, $num);

        if (!$order) {
            throw new OrderException();
        }

        return $order;
    }

    /**
     * 创建商城订单
     * @return mixed
     */
    public function createOrderBuy() {
        (new IDMustBePostiveInt())->goCheck();
        (new OrderValidate())->goCheck('attr_group_id');
        (new OrderValidate)->goCheck('create_order');

        $id = input('param.id');
        $attr_group_id = input('param.attr_group_id');
        $num  = input('param.num', 1);
        $address_id = input('param.address_id');
        $source = input('param.source');

        if ($num > 1) (new OrderValidate())->goCheck('num');

        $order = OrderBuyModel::createOrderBuy($id, $attr_group_id, $num , $address_id, $source);
        return $order;
    }

    /**
     * 获取商城订单详情
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws OrderException
     */
    public function getOrderBuyInfo($id) {
        (new IDMustBePostiveInt())->goCheck();

        $order = OrderBuyModel::getOrderBuyInfo($id);
        if (!$order) {
            throw new OrderException();
        }
        $order = $order->toArray();
        return $order;
    }

    /**
     * 根据ID删除商城订单
     * @return int
     * @throws OrderException
     */
    public function deleteOrderBuy() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $order = OrderBuyModel::deleteOrderBuy($id);
        if (!$order) {
            throw new OrderException([
                'msg' => '订单删除失败',
                'errorCode' => 60003
            ]);
        }
        return json(new SuccessMessage(), 201);
    }

    /**
     * 确认收货
     * @return \think\response\Json
     * @throws OrderException
     */
    public function orderBuyConfirmReceipt() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        $order = OrderBuyModel::orderBuyConfirmReceipt($id);
        if (!$order) {
            throw new OrderException([
                'msg' => '确认收货失败',
                'errorCode' => 60004
            ]);
        }
        return json(new SuccessMessage(), 201);
    }

    /**
     * 获取申请售后页面数据
     * @param $id
     * @return array
     */
    public function applyAftermarketData($id) {
        (new IDMustBePostiveInt())->goCheck();
        $data = OrderBuyAfterSaleModel::applyAftermarketData($id);
        return $data;
    }

    /**
     * 创建售后单
     * @return \think\response\Json
     */
    public function createAftermarket() {
        $orderAftermarketValidate = new OrderAftermarketValidate();
        $orderAftermarketValidate->goCheck('create');
        $params = $orderAftermarketValidate->getDataByRule(input('post.'));

        if (input('?post.image')) {
            $image = input('post.image/a');
            $params['image'] = $image;
        }

        OrderBuyAfterSaleService::createAftermarket($params);

        return json(new SuccessMessage(), 201);
    }

    /**
     * 获取商城订单售后列表
     * @return \think\Paginator
     */
    public function afterSaleList() {
        $after_sale_list = OrderBuyAfterSaleModel::afterSaleList()->toArray();
        return $after_sale_list;
    }

    /**
     * 获取售后单详情
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws OrderException
     */
    public function afterSaleInfo($id) {
        (new IDMustBePostiveInt())->goCheck();
        $data = OrderBuyAfterSaleModel::afterSaleInfo($id);
        if (!$data) {
            throw new OrderException([
                'meg' => '售后单不存在',
                'errorCode' => 60007
            ]);
        }
        return $data;
    }

    /**
     * 用户保存售后单物流信息
     * @return \think\response\Json
     */
    public function saveLogistics() {
        (new OrderAftermarketValidate())->goCheck('logistics');
        $id = input('param.id');
        $member_express_sn = input('param.member_express_sn');
        OrderBuyAfterSaleModel::saveLogistics($id, $member_express_sn);
        return json(new SuccessMessage(), 201);
    }

    /**
     * 售后单确认收货
     * @return \think\response\Json
     */
    public function afterSaleConfirmReceipt(){
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        OrderBuyAfterSaleModel::afterSaleConfirmReceipt($id);
        return json(new SuccessMessage(), 201);
    }

    /**
     * 取消售后单
     * @return \think\response\Json
     */
    public function cancelAfterSale() {
        (new IDMustBePostiveInt())->goCheck();
        $id = input('param.id');
        OrderBuyAfterSaleModel::cancelAfterSale($id);
        return json(new SuccessMessage(), 201);
    }

    /**
     * 获取物流信息
     * @param $express_sn
     * @return array
     */
    public function expressInfo($express_sn) {
        (new ExpressValidate())->goCheck();
        $express_info = OrderBuyModel::expressInfo($express_sn);
        return $express_info;
    }
}